<?php
/**
 * TinyCorePHP Control基类
 * @author 	itkele@gmail.com
 * @date 	2013-02-17
 */
class Control{
	public $vars = array();
	public $tmpl_dir = 'templates';

	public function __construct(){
		//
	}
	
	public function assign($key,$val){
		$this->vars[$key] = $val;
	}
	
	public function display($tmpl,$tmpl_dir=null){
		if(is_array($this->vars)) {
        	extract($this->vars, EXTR_SKIP);
      	}
		$default_theme = C('tmpl.default_theme');
		$class_name = get_class($this);
		$tmpl_dir = empty($tmpl_dir) ? substr($class_name,0,-7) : $tmpl_dir;
      	$tmpl_dir = strtolower($tmpl_dir);
		$file = APP_ROOT . '/' . $this->tmpl_dir . '/' . $default_theme . '/' . $tmpl_dir . '/' . $tmpl.'.php';
		
		$components = (string)trim($_GET['com']);
		if( ! empty($components)){
			$file = APP_ROOT . '/components/com_' . strtolower($components) .'/' . $this->tmpl_dir . '/' . $default_theme . '/' . $tmpl_dir . '/' . $tmpl.'.php';
		}else{
			$file = APP_ROOT . '/' . $this->tmpl_dir . '/' . $default_theme . '/' . $tmpl_dir . '/' . $tmpl.'.php';
		}		

      	if(file_exists($file)){
      		@include $file;
			//调试信息
			if(C('app_debug')){
				echo '<div style="width:100%;height:auto;overflow:auto;"><pre style="float:left;width:100%;line-height:20px;margin-top:10px;border:1px dashed #ccc;border-left:0;border-right:0;"><b>调试信息：</b><br />';
				debug_print_backtrace();
				echo '</pre></div>';
			}
      	}else{
      		die('Template file is not found.');
      	}
	}
}
?>